#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;

class Solution1 {
public:
    int minmumNumberOfHost(int n, vector<vector<int> >& startEnd) {
        map<int, int> mp;
        for (auto& st : startEnd)
        {
            mp[st[0]]++;
            mp[st[1]]--;
        }
        int res = 0; int cur = 0;
        for (auto& item : mp)
        {
            cur += item.second;
            res = max(res, cur);
        }
        return res;
    }
};

class Solution2 {
public:
    int findMinimumTime(vector<vector<int>>& tasks) {
        sort(tasks.begin(), tasks.end(), [](const vector<int>& t1, const vector<int>& t2) {
            return t1[1] < t2[1];
            });
        vector<int> run(tasks.back()[1] + 1);
        int res = 0;
        for (auto& task : tasks)
        {
            int start = task[0]; int end = task[1]; int duration = task[2];
            int sum = 0;
            for (int i = start; i <= end; ++i) sum += run[i];
            duration -= sum;
            res += max(duration, 0);
            for (int j = end; j >= 0 && duration > 0; j--)
            {
                if (run[j] == 0)
                {
                    duration--;
                    run[j] = 1;
                }
            }
        }
        return res;
    }
};

using pii = pair<int, int>;

int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,-1,1 };

int main()
{
    int n, m;
    cin >> n >> m; getchar();
    int xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt; getchar();
    xs--; ys--; xt--; yt--;
    vector<string> g(n);
    for (int i = 0; i < n; ++i)
        getline(cin, g[i]);

    queue<pii> q; q.push({ xs,ys }); g[xs][ys] = '*';
    int res = 0;
    while (!q.empty())
    {
        int sz = q.size(); res++;
        while (sz--)
        {
            auto p = q.front(); q.pop();
            for (int k = 0; k < 4; ++k)
            {
                int x = p.first + dx[k]; int y = p.second + dy[k];
                if (x >= 0 && y >= 0 && x < n && y < m && g[x][y] == '.')
                {
                    if (x == xt && y == yt)
                    {
                        cout << res;
                        return 0;
                    }
                    q.push({ x,y }); g[x][y] = '*';
                }
            }
        }
    }

    cout << -1;

    return 0;
}

